#!/bin/bash

# Post commit hook to force dspec to always have current commit hash 
TARGET_FILE="VSoft.DUnitX.dspec"


# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}Post-commit hook: Processing ${TARGET_FILE}${NC}"

# Check if the target file exists
if [ -f "$TARGET_FILE" ]; then
    echo -e "${RED}Deleting ${TARGET_FILE}${NC}"
    rm "$TARGET_FILE"
    
    # Verify deletion
    if [ ! -f "$TARGET_FILE" ]; then
        echo -e "${GREEN}File successfully deleted${NC}"
    else
        echo -e "${RED}Error: Failed to delete ${TARGET_FILE}${NC}"
        exit 1
    fi
else
    echo -e "${YELLOW}File ${TARGET_FILE} does not exist, skipping deletion${NC}"
fi

# Force checkout the file from the repository
echo -e "${YELLOW}Force checking out ${TARGET_FILE} from HEAD${NC}"

# Use git checkout to restore the file from the latest commit
git checkout HEAD -- "$TARGET_FILE"

# Check if the checkout was successful
if [ $? -eq 0 ]; then
    if [ -f "$TARGET_FILE" ]; then
        echo -e "${GREEN}Successfully restored ${TARGET_FILE} from repository${NC}"
    else
        echo -e "${YELLOW}File ${TARGET_FILE} was not restored (may not exist in repository)${NC}"
    fi
else
    echo -e "${RED}Error: Failed to checkout ${TARGET_FILE}${NC}"
    exit 1
fi

echo -e "${GREEN}Post-commit hook completed successfully${NC}"